package com.itheima;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;

/**
 * @ProjectName: untitled
 * @Titile: ArrayListDemo
 * @Author: 王亦凡1
 * @Description: TODO
 */
public class ArrayListDemo {
    public static void main(String[] args){
        ArrayList list=new ArrayList();
        list.add("张三");//添加一个数据在数组的尾部
        list.add(0,"list");//添加一个数据在数组指定位置
        //获取数据
        String s = (String) list.get(1);
        System.out.println(s);
        //修改数据
        list.set(0,"wangwu");
        list.remove(1);
        list.add("zhangsan");
        list.add("lisi");
        list.add("wangwu");


        System.out.println(list);
        System.out.println("---------------集合遍历的方法------------------------");


        //1.for循环的遍历方式
        System.out.println("集合遍历方法1");
        for(int i=0;i<list.size();i++){
            String obj= (String) list.get(i);
            System.out.println(obj);
        }
        //2.增强for循环的形成
        System.out.println("集合遍历方法2");
        for(Object obj :list){
            System.out.println(obj);
        }

        //3.迭代器的形成
        System.out.println("集合遍历方法3");
        //获取集合的迭代器对象
        Iterator<String> iterator =list.iterator();
        while (iterator.hasNext()){
            String next =iterator.next();
            System.out.println(next);
        }
        //4.Lambda表达式
        System.out.println("集合遍历方法4");
        //Lambda表达式的本质是一个函数
        Consumer<String>consumer = new Consumer<String>(){
            @Override
            public void accept(String s){
                System.out.println(s);
            }

        };
        list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
              System.out.println(s);
            }
        });
        list.forEach(name->System.out.println(name));

        list.forEach(System.out::println);

    }

}
